//
//  YZMMasterCell.m
//  YZMicroStock
//
//  Created by Lv.Dongdong on 16/11/4.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMMasterCell.h"

@interface YZMMasterCell()

@property (nonatomic, strong) UILabel *rankLB;
@property (nonatomic, strong) UILabel *stockNameLB;
@property (nonatomic, strong) UILabel *stockAbbLB;
@property (nonatomic, strong) UILabel *timeLB;
@property (nonatomic, strong) UILabel *tradeCountLB;

@end

@implementation YZMMasterCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createObjects];
        self.rightMargin = 10.0;
        self.leftMargin = 10.0;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return self;
}

- (void)createObjects{
    
    WS(ws)
    self.rankLB = [YZMAppStyle labelWithRedBGAndWhiteFont];
    self.rankLB.font = [YZMAppStyle fontWithSize:9];
    self.rankLB.backgroundColor = [YZMAppStyle colorForRedMark];
    [self.contentView addSubview:self.rankLB];
    [self.rankLB mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.contentView).offset(20);
        make.width.mas_equalTo(16);
        make.height.mas_equalTo(12);
        make.top.equalTo(ws.contentView).offset(15);
    }];
    
    UIView *helperView = [UIView new];
    [self.contentView addSubview:helperView];
    [helperView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.centerY.equalTo(ws.contentView);
    }];
    
    self.timeLB = [YZMAppStyle labelWithMediumWhite];
    self.timeLB.adjustsFontSizeToFitWidth = YES;
    self.timeLB.minimumScaleFactor = 0.1;
    [helperView addSubview:self.timeLB];
    [self.timeLB mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.centerX.equalTo(helperView);
        make.right.lessThanOrEqualTo(helperView);
    }];
    
    self.tradeCountLB = [YZMAppStyle labelWithSmallLightGray];
    [helperView addSubview:ws.tradeCountLB];
    [self.tradeCountLB mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(ws.timeLB);
        make.bottom.right.lessThanOrEqualTo(helperView);
        make.top.equalTo(ws.timeLB.mas_bottom).offset(3);
    }];
    
    UIView *stockView = [UIView new];
    [self.contentView addSubview:stockView];
    [stockView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(ws.rankLB.mas_right).offset(6);
        make.centerY.equalTo(ws.contentView);
        make.right.equalTo(helperView.mas_left).offset(-8);
    }];
    
    self.stockNameLB = [YZMAppStyle labelWithMediumWhite];
    [stockView addSubview:self.stockNameLB];
    [self.stockNameLB mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.centerX.top.equalTo(stockView);
    }];
    
    self.stockAbbLB = [YZMAppStyle labelWithSmallLightGray];
    self.stockAbbLB.adjustsFontSizeToFitWidth = YES;
    self.stockAbbLB.minimumScaleFactor = 0.1;
    [stockView addSubview:ws.stockAbbLB];
    [self.stockAbbLB mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.centerX.equalTo(ws.stockNameLB);
        make.top.equalTo(ws.stockNameLB.mas_bottom).offset(3);
        make.bottom.equalTo(stockView);
    }];
    
    UIButton *buyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [buyBtn setTitle:@"我要点买" forState:UIControlStateNormal];
    [buyBtn setTitleColor:[YZMAppStyle colorForIconColor] forState:UIControlStateNormal];
    buyBtn.titleLabel.font = [YZMAppStyle mediumSizeFont];
    buyBtn.layer.borderWidth = 1;
    buyBtn.layer.borderColor = [YZMAppStyle colorForIconColor].CGColor;
    buyBtn.layer.cornerRadius = 2;
    [buyBtn addTarget:self action:@selector(pushBuyStock:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:buyBtn];
    [buyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(ws.contentView).offset(-25);
        make.centerY.equalTo(ws.contentView);
        make.height.mas_equalTo(22);
        make.width.mas_equalTo(70);
    }];

    

}

-(void)setModel:(YZMHomeStrategyModel *)model
{
    self.stockNameLB.text =model.stockName;
    self.stockAbbLB.text =model.stockCode;
    self.timeLB.text = @"1小时前累计";
    self.rankLB.text=[model.stockCode substringToIndex:2];
    self.tradeCountLB.text = [NSString stringWithFormat:@"交易%d股",model.buyCount];
    _model = model;
}

- (void)pushBuyStock:(UIButton *)send {
    
    [YZMHCMInstance pushViewControllerWithName:@"stock" andParams:@{@"stockcode":self.model.stockCode}];
}
                                                                             
@end
